var util = module.exports;


//获取从[1 - maxNum)中num个不同数字
util.getArrayItems = function(maxNum,num) {
    var temp_array = new Array();
    for (var i = 0 ;i < maxNum; i++) {
        temp_array[i] = i
    }
    //取出的数值项,保存在此数组
    var return_array = new Array();
    for (var i = 0; i<num; i++) {
        //判断如果数组还有可以取出的元素,以防下标越界
        if (temp_array.length>0) {
            //在数组中产生一个随机索引
            var arrIndex = Math.floor(Math.random()*temp_array.length);
            //将此随机索引的对应的数组元素值复制出来
            return_array[i] = temp_array[arrIndex];
            //然后删掉此索引的数组元素,这时候temp_array变为新的数组
            temp_array.splice(arrIndex, 1);
        } else {
            //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
            break;
        }
    }
    return return_array;
}

//判断两个时间相差天数
util.dayCompare = function(start,end){
    var time1 = Date.parse(start);
    var time2 = Date.parse(end);
    var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24));
    return  nDays;
}
//获取今日时间
util.getDateString = function(inc) {
    var myDate = new Date()
    if(inc){
        myDate.setDate(myDate.getDate() + inc)
    }
    var month = myDate.getMonth() + 1
    var date = myDate.getDate()
    if(month < 10){
        month = "0"+month
    }
    if(date < 10){
        date = "0"+date
    }
    var dateString = parseInt(""+myDate.getFullYear() + month + date)
    return dateString
}


util.getMinutes = function() {
    var myDate = new Date()
    return myDate.getHours() * 60 + myDate.getMinutes()
}


//生成随机token
util.randomString = function(len){
　　len = len || 32;  
　　var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';  
　　var maxPos = $chars.length;  
　　var pwd = '';  
　　for (i = 0; i < len; i++) {  
        //0~32的整数  
　　　　pwd += $chars.charAt(Math.floor(Math.random() * (maxPos+1)));  
　　}  
　　return pwd;  
}  


util.deepCopy = function(source) {
  var result={}
  for (var key in source) {
        result[key] = typeof source[key]==="object"? util.deepCopy(source[key]): source[key]
     } 
  return result;
}
util.deepCopyArr = function(source) {
    var result = source
    if(typeof(source) =="object"){
        if(typeof(source) =="object" && source.length !== undefined){
            result = []
            for(var index = 0;index < source.length;index++) {
                result.push(typeof source[index]==="object"? util.deepCopyArr(source[index]): source[index])
            }
        }else{
            result={}
            for(var index in source) {
                result[index] = typeof source[index]==="object"? util.deepCopyArr(source[index]): source[index]
            }
        }
    }
    return result;
}